/*
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-07 10:43:07
 * @LastEditors: 颜礼田 2564217790@qq.com
 * @LastEditTime: 2024-09-07 18:08:09
 * @FilePath: /webServer/src/utils/ormHelper.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { Like } from 'typeorm';
import { isNotEmpty, isObject, isString } from './isHelper';
export function createSearch(search?: object) {
  if (!search) return {};

  return Object.entries(search).reduce((searchWhere, [key, element]) => {
    if (isNotEmpty(element)) {
      searchWhere[key] = isObject(element)
        ? createSearch(element)
        : isString(element)
        ? Like(`%${element}%`)
        : element;
    }
    return searchWhere;
  }, {});
}

export function createOrder(field = 'createAt', sort = 'DESC') {
  return { [field]: sort };
}
